package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberDetailService;
import com.itheima.untils.ModeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequestMapping("/memberDetail")
@RestController
public class MemberDetailController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private MemberDetailService memberDetailService;
    @RequestMapping("/findAll")
    public Result findAll(HttpServletRequest request){
        try {
            //获取用户手机号
            String telephone = null;
            Cookie[] cookies = request.getCookies();
            for (Cookie cookie : cookies) {
                String na = cookie.getName();
                //对cookie进行解密
                String name = null;
                try {
                    //使用des工具类进行des解密
                    name = ModeUtils.decryptDES(na);
                } catch (Exception e) {
                    continue;
                }
                if (name.equals("login_member_telephone")){
                    telephone = cookie.getValue();
                }
            }
            //根据手机号查询缓存回显数据
            String json = jedisPool.getResource().get(telephone);
            //json转回member对象
            Member member = JSONObject.parseObject(json, Member.class);
            return new Result(true,"信息查询成功",member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"信息查询失败");
        }
    }
    /*
    * 修改个人信息
    * */
    @RequestMapping("/updateMember")
    public Result updateMember(@RequestBody Member member, String validateCode, HttpServletResponse response){
        try {
            //获取手机号
            String telephone = member.getPhoneNumber();
            //获取缓存中的验证码
            String str = jedisPool.getResource().get(telephone+ RedisMessageConstant.SENDTYPE_ORDER);
            if (validateCode==null){
                return new Result(false,"密码不能为空");
            }
            if (!str.equals(validateCode)){
                return new Result(false,"验证码错误");
            }
            Result result =  memberDetailService.updateMember(member);
            //将存入cookie的key进行DES加密
            String coe = "login_member_telephone";
            String encode = ModeUtils.encryptDES(coe);
            //把手机号存入cookie中
            Cookie cookie = new Cookie(encode,member.getPhoneNumber());
            cookie.setPath("/");
            cookie.setMaxAge(60*60*24*30);//30天
            response.addCookie(cookie);
            //将数据存入缓存中
            //把member转化为json
            String json = JSON.toJSON(member).toString();
            jedisPool.getResource().setex(telephone,60*30,json);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑失败");
        }
    }
}
